//# sourceURL=http://uMapApp/Module/Road/RoadSection
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth",
    "../../UI/Layer",
    "../../Earth/ViewPort"
], function (
    mhjs,
    uMap,
    uEarth,
    Layer,
    ViewPort
) {

    function RoadSection(option) {
        var _this = this;
        option && _this.OptionToProperties(option);
    }

    var _class = RoadSection;

    mhjs.OptionToPropertiesInterface(_class);
    mhjs.EventInterface(_class);

    Object.defineProperties(_class.prototype, {
        "open"                 : {
            value: function () {
                if (!this.isOpened) {
                    // this.layer && this.layer.open();
                    this.isOpened = true;
                    this.trigger("open");
                }
            }
        },
        "close"                : {
            value: function () {
                if (this.isOpened) {
                    this.layer && this.layer.close();
                    this.isOpened = false;
                    this.trigger("close");
                }
            }
        },
        "toggle"               : {
            value: function () {
                if (this.isOpened) {
                    this.close();
                } else {
                    this.open();
                }
            }
        },
        "startStakeNum"        : {
            get: function () {
                return this._startStakeNum;
            },
            set: function (value) {

                var _this = this;
                _this._startStakeNum = _this.transformStakeNum(value);
                _this.updateStakeNums();

            }
        },
        "stopStakeNum"         : {
            get: function () {
                return this._stopStakeNum;
            },
            set: function (value) {
                var _this = this;
                _this._stopStakeNum = _this.transformStakeNum(value);
                _this.updateStakeNums();
            }
        },
        "roadTrackNum"         : {
            get: function () {
                return this._roadTrackNum;
            },
            set: function (value) {

                var _this = this;
                _this._roadTrackNum = value;
                _this.updateStakeNums();

            }
        },
        "transformStakeNum"    : {
            value: function (stakeNum) {
                var _this = this;

                var pattern = /(K|k)\d{1,}(\+\d{1,3})*/;

                if (pattern.test(stakeNum)) {
                    stakeNum = stakeNum.replace(/K|k/, "K");
                    return {
                        "no" : Number(stakeNum.split("+")[0].split("K")[1]),
                        "dis": Number(stakeNum.split("+")[1] || 0),
                        "str": stakeNum
                    };
                }

            }
        },
        "updateStakeNums"      : {
            value: function () {
                var _this = this;
                if (_this.startStakeNum && _this.stopStakeNum) {
                    // K75 K110+900
                    // K75 K110+900 1 U

                    //整体
                    if (!_this.roadTrackNum) {
                        _this.rightLines = [];
                        _this.leftLines = [];
                        for (var i = _this.startStakeNum.no; i <= _this.stopStakeNum.no; i++) {

                            if (i !== _this.stopStakeNum.no || (i === _this.stopStakeNum.no && _this.stopStakeNum.dis > 0)) {

                                _this.rightLines.push({
                                    "startStakeNum": i,
                                    "endStakeNum"  : i + 1,
                                    "roadLineNum"  : 0,
                                    "directionCode": "U"
                                });
                                _this.leftLines.push({
                                    "startStakeNum": _this.stopStakeNum.no + _this.startStakeNum.no - i - 1,
                                    "endStakeNum"  : _this.stopStakeNum.no + _this.startStakeNum.no - i,
                                    "roadLineNum"  : 0,
                                    "directionCode": "D"
                                });

                            }


                        }

                        $.each(_this.rightLines, function (i, roadLine) {
                            _this.app.servers.DC.requests.roadLine.send(roadLine, function (json) {
                                _this.rightLines[i].positions = json.data.list[0].dataObj.positions;
                                _this.rightLines[i].isLoaded = true;
                                _this.checkLoaded();
                            });

                        });

                        $.each(_this.leftLines, function (i, roadLine) {
                            _this.app.servers.DC.requests.roadLine.send(roadLine, function (json) {
                                _this.leftLines[i].positions = json.data.list[0].dataObj.positions;
                                _this.leftLines[i].isLoaded = true;
                                _this.checkLoaded();
                            });

                        });

                    } else {

                    }


                }
            }
        },
        "checkLoaded"          : {
            value: function () {
                var _this = this;

                var total = _this.rightLines.length + _this.leftLines.length;
                var loaded = 0;

                $.each(_this.rightLines, function (i, roadLine) {
                    if (roadLine.isLoaded) {
                        loaded += 1;
                    }
                });
                $.each(_this.leftLines, function (i, roadLine) {
                    if (roadLine.isLoaded) {
                        loaded += 1;
                    }
                });

                if (loaded === total) {
                    _this.getRoadSectionPolygon();
                }

            }
        },
        "mergeRoadLines"       : {
            value: function (roadLines) {
                var _this = this;
                if (_this.startStakeNum && _this.stopStakeNum) {

                    // K95 K110+900

                    _this.stakeNums = [];
                }
            }
        },
        "getRoadSectionPolygon": {
            value: function () {
                var _this = this;

                _this.polygonPositions = [];

                $.each(_this.rightLines, function (i, roadLine) {
                    $.each(roadLine.positions, function (j, roadLinePosition) {
                        if (i === _this.rightLines.length || j < roadLine.positions.length) {
                            _this.polygonPositions.push(roadLinePosition);
                            console.log(roadLine.startStakeNum + "" + j);
                            console.log(roadLinePosition);
                        }
                    });
                });

                $.each(_this.leftLines, function (i, roadLine) {
                    $.each(roadLine.positions, function (j, roadLinePosition) {
                        if (i === 0 || j !== 0) {
                            _this.polygonPositions.push(roadLine.positions[roadLine.positions.length - j - 1]);
                            console.log(roadLine.startStakeNum + "" + j);
                            console.log(roadLine.positions[roadLine.positions.length - j - 1]);
                        }
                    });
                });

                console.log(_this.polygonPositions);

            }
        },
        "active"               : {
            value: function (isActive) {
                var _this = this;

                isActive = (isActive === undefined) || Boolean(isActive);

                if (isActive) {
                    // _this.layer.active();
                    // _this.polyline.polyline.material.color = new uMap.Color(["#ff0"]);
                    // _this.trigger("active");
                } else {
                    // _this.polyline.polyline.material.color = new uMap.Color(["#fff", 0.5]);
                    // _this.trigger("unActive");
                }

                _this.isActive = isActive;


            }
        }
    });

    return _class;

});